// pages/giftShare/giftShare.js
const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    card: [],
    total_num:0,
    getCardSuccess:false,
    getHopsSuccess:false,
    showSendFriend:true,  //显示赠送朋友、
     remarkCol:true, //备注弹窗
     remarkText:'', //备注信息
     text:'可输入备注信息，方便您自己查看'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log('参数',options)
    if (options.uid){
      var share_uid = options.uid
    }else{
      share_uid=''
    }
    if (options.shareType) {
      var shareType = options.shareType
      // console.log(shareType);
    } else {
      shareType = ''
    }
    wx.hideShareMenu() // 隐藏分享按钮
    var userinfo = wx.getStorageSync('accountInfo');
    var uid = userinfo.uid;
    this.setData({
      show_type: options.show_type,
      linkCode: options.linkCode,
      uid:uid,
      share_uid: share_uid,
      getCardSuccess:false,
      shareType: shareType
    })
  },

  onShow:function(){
    var userinfo = wx.getStorageSync('accountInfo');
    var uid = userinfo.uid;
    this.setData({
      uid: uid
    })
     if (this.data.shareType == 1) {
        this.hopsInfo();
     } else {
        this.cardInfo()
     }
  },

// 酒花信息获取
hopsInfo:function(){
  var that = this;
  api.POST({
    params: {
      linkCode: that.data.linkCode
    },
    url: app.globalData.base_url + 'hops/statusInfo',
    success: function (res) {
      if (util.isEmpty(res.data.code)) {
        var hopsInfo = res.data.data.result;
        // console.log(card)
        that.setData({
          hopsInfo: hopsInfo
        })
        // that.doData(card.list);
      } else {
        util.showError(res.data.message);
      }
    },
    fail: function (fail) {
      util.showError("网络错误，请检查网络");
    }
  })
},

// 领取酒花
  getHops:function(){
    var that = this;
    api.POST({
      params: {
        linkCode: that.data.linkCode
      },
      url: app.globalData.base_url + 'hops/getHops',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          that.setData({
            getHopsSuccess: true
          })
        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("网络错误，请检查网络");
      }
    })
  },

  toMyHops: function () {
    var sku_code = this.data.hopsInfo.sku_code;
    app.globalData.sku_code = sku_code;
    app.globalData.winType=2;
    wx.switchTab({
      url: '/pages/wineNew/wineNew'
    });
  },

  //礼品卡信息
  cardInfo: function () {
    var that = this;
    api.POST({
      params: {
        linkCode: that.data.linkCode
      },
      url: app.globalData.base_url + 'Giftcard/shareStatus',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {

          // console.log(res.data);
            var couponsArr = [];
            var winArr = [];
           if (res.data.data.remark){
              that.setData({
                 text: res.data.data.remark,
                 remarkText: res.data.data.remark
              })
           }

          if (res.data.data.type==1){
            var card = res.data.data;

            if (card.list.length>1){
              var active_type = 1
            } else if (card.list.length == 1 && card.list[0].expired==1){
              var active_type = 0
            }else{
              var active_type = 1
            }

             var arr = card.list.find(function(item){
                return item.type==2
             })
             if (arr instanceof Object){
                couponsArr.push(arr)
                arr=null
             }

             var arr2 = card.list.find(function (item) {
                return item.type == 1
             })
             if (arr2 instanceof Object) {
                winArr.push(arr)
                arr2 = null
             }

            that.setData({
              card: card,
              none_list: card.list,
              cardType:1,
              active_type: active_type
            })
            // that.doData(card.list);
          } else if (res.data.data.type == 2){
            var card = res.data.data;
            if (card.user_type==2 && card.status == 2){  //领取人查看
              
              if (card.list.length > 1) {
                var active_type = 1
              } else if (card.list.length == 1 && card.list[0].expired == 1) {
                var active_type = 0
              } else {
                var active_type = 1
              }

               var arr = card.list.find(function (item) {
                  return item.type == 2
               })
               if (arr instanceof Object) {
                  couponsArr.push(arr)
                  arr = null
               }

               var arr2 = card.list.find(function (item) {
                  return item.type == 1
               })
               if (arr2 instanceof Object) {
                  winArr.push(arr)
                  arr2 = null
               }

              that.setData({
                card: card,
                none_list: card.list,
                cardType: 2,
                user_type: card.user_type,
                active_type: active_type
              })
            } else if (card.user_type == 2) {  //领取人查看
               if (card.list instanceof Array){
                  var arr = card.list.find(function (item) {
                     return item.type == 2
                  })
                  var arr2 = card.list.find(function (item) {
                     return item.type == 1
                  })
               }
               
               if (arr instanceof Object) {
                  couponsArr.push(arr)
                  arr = null
               }
               if (arr2 instanceof Object) {
                  winArr.push(arr)
                  arr2 = null
               }

              that.setData({
                card: card,
                none_list: card.list,
                cardType: 2,
                user_type: card.user_type,
              })
            }  else if (card.user_type == 1){  //自己查看

               var arr = card.none_list.find(function (item) {
                  return item.type == 2
               })
               if (arr instanceof Object) {
                  couponsArr.push(arr)
                  arr = null
               }

               var arr2 = card.none_list.find(function (item) {
                  return item.type == 1
               })
               if (arr2 instanceof Object) {
                  winArr.push(arr)
                  arr2 = null
               }
               

               for (let i = 0; i < card.get_list.length;i++){
                  var arr = card.get_list[i].goods_list.find(function (item) {
                     return item.type == 2
                  })
                  if (arr instanceof Object) {
                     couponsArr.push(arr)
                     arr = null
                  }

                  var arr2 = card.get_list[i].goods_list.find(function (item) {
                     return item.type == 1
                  })
                  if (arr2 instanceof Object) {
                     winArr.push(arr)
                     arr2 = null
                  }
                  

               }

               

               var arr = card.back_list.find(function (item) {
                  return item.type == 2
               })
               if (arr instanceof Object) {
                  couponsArr.push(arr)
                  arr = null
               }

               var arr2 = card.back_list.find(function (item) {
                  return item.type == 1
               })
               if (arr2 instanceof Object) {
                  winArr.push(arr)
                  arr2 = null
               }

              that.setData({
                card: card,
                none_list: card.none_list,
                get_list: card.get_list,
                back_list: card.back_list,
                total_num: card.num,
                get_num: card.get_num,
                cardType: 2,
                user_type: card.user_type
              })
            }
          }

         //   console.log(couponsArr);
         that.setData({
            couponsArr: couponsArr,
            winArr: winArr
         })

        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("网络错误，请检查网络");
      }
    })
  },

  goLink: function () {
    wx.switchTab({
      url: '/pages/index/index'
    });
  },

  getCard: function () {
    var that = this;
    api.POST({
      params: {
        linkCode: that.data.linkCode
      },
      url: app.globalData.base_url + 'Giftcard/shareSub',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          that.setData({
            getCardSuccess:true
          })
          that.cardInfo();
        } else if (res.data.code=='80021'){
          that.cardInfo();
          util.showError(res.data.message);
        }
         else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("网络错误，请检查网络");
      }
    })
  },

  //处理数据
  doData: function (list) {
    var total_num=0;
    for (var i = 0; i < list.length; i++) {
      total_num += list[i].num
    }
    this.setData({
      total_num: total_num
    })
  },

  toMyCard:function(){
    app.globalData.linkCode = this.data.card.linkCode;
    wx.redirectTo({
      url: '/play/pages/myGift/myGift?show_type=2&card_type=2&cardLink=1'
    })
  },

  onShareAppMessage: function (res) {
    var that = this;
    var userinfo = wx.getStorageSync('accountInfo');
    var cardInfo = that.data.card;
    var nickname = userinfo.nickname;
    if (!nickname){
      nickname=''
    }
    setTimeout(function () {
      that.setData({
        showSendFriend: false
      })
    }, 1000)
    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: '好友"' + nickname + '"送您"' + cardInfo.name + '"礼品卡',
        path: '/play/pages/giftShare/giftShare?uid=' + that.data.uid+'&show_type=4&linkCode=' + cardInfo.linkCode,
        imageUrl: cardInfo.share_img,
        complete(){
        }
      }
    }
  },


  winLink:function(e){
    var sku_code = e.currentTarget.dataset.sku_code;
    var show_type = this.data.show_type;
   //  app.globalData.sku_code = sku_code;
   //  app.globalData.winType=1;
    if (show_type==3){
      return
    }
     if (!sku_code){
        return
     }
    wx.navigateTo({
       url: '/index/pages/productDetails/productDetails?sku_code=' + sku_code
    })
   //  wx.switchTab({
   //    url: '/pages/wineNew/wineNew'
   //  });
  },
  //撤销礼品卡
  undo: function (e) {
    var that = this;
    var linkCode = that.data.card.linkCode
    wx.showModal({
      title: '提示',
      content: '是否确认撤销？',
      success(res) {
        if (res.confirm) {
          api.POST({
            params: {
              linkCode: linkCode,
            },
            url: app.globalData.base_url + 'giftcard/backout',
            success: function (res) {
              if (util.isEmpty(res.data.code)) {
                util.showSuccess("撤销成功");
                that.cardInfo();
                setTimeout(function(){
                   wx.navigateBack({
                      delta: 1
                   })
                },1500)
              } else if (res.data.code=="80018"){
                util.showError(res.data.message);
                that.cardInfo()
              }
            }
          })
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
  },
  
  //再送一张
  goBack:function(){
      wx.navigateBack({
        delta:1
      })
  },

  myWine:function(){
    wx.switchTab({
      url: '/pages/wineNew/wineNew'
    })
  },
   myCoupons: function () {
      wx.navigateTo({
         url: '/center/pages/coupons/coupons'
      })
   },
  onHide:function(){
    var that = this;
  },
  //关闭备注弹窗
   remarkCancel:function(){
      this.setData({
         remarkCol:true
      })
   },
   //打开备注弹窗
   remarkOpen: function () {
      this.setData({
         remarkCol: false
      })
   },
   //提交备注信息
   remarkSubmit: function () {
      let that = this;
      let remarkText = this.data.remarkText
      let linkCode = this.data.linkCode
      if (remarkText){
         api.POST({
            params: {
               linkCode: linkCode,
               remark: remarkText
            },
            url: app.globalData.base_url + 'giftcard/remark',
            success: function (res) {
               if (util.isEmpty(res.data.code)) {
                  that.setData({
                     text: remarkText,
                     remarkCol:true
                  })
               } else {
                  util.showError(res.data.message);
               }
            }
         })
      }
   },
   //输入备注
   remarkTextInput:function(e){
      let valve = e.detail.value
      this.setData({
         remarkText: valve
      })
   }
})